package com.j2009.springbootproject.web;

import com.j2009.springbootproject.entity.Attendant;
import com.j2009.springbootproject.entity.ResponseResult;
import com.j2009.springbootproject.entity.User;
import com.j2009.springbootproject.service.AttandentService;
import com.j2009.springbootproject.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ClassName: UserController
 * Description:
 * date: 2021/3/25 15:27
 *
 * @author wuyafeng
 * @version 1.0   softeem.com
 */
@RequestMapping("/adminUser")
@Controller
public class UserController {

    @Autowired
    private AttandentService service;

    @RequestMapping("/toLogin")
    public String toLogin(){
        System.out.println("进入login页面");
        return "admin/login";  //默认访问  template/admin/login.html
    }


    @ResponseBody
    @RequestMapping("/login")
    public ResponseResult login(@RequestParam("username") String username,
                                @RequestParam("password") String password,
                                HttpSession session, Model model){
        ResponseResult result = new ResponseResult();
        if(username.equals("admin") && "123456".equals(password)){
            //登录成功
            Attendant attendant = new Attendant();
            attendant.setAname(username);
            attendant.setPassword(password);
           // session.setAttribute("user",attendant);

            User user = new  User();

            //响应 token
            JWTUtils.createJWT(180,user);

            result.setCode(0);
        }else{

            result.setCode(-1);
            result.setMessage("用户名或密码错误");
        }
        return result;
    }

    @RequestMapping("/toMain")
    public String toMain(){
        return "admin/main";
    }

    @RequestMapping("/toUser")
    public String toUser(){
        return "admin/user";
    }

    @RequestMapping("/toAddUser")
    public String toAddUser(){
        return "admin/admin_add";
    }


    @ResponseBody
    @RequestMapping("/list")
    public List<Attendant> list(){
       return  service.list();
    }

    @ResponseBody
    @RequestMapping("/getById")
    public Attendant getById(int id){
        return service.getById(id);
    }

}
